﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace SistemaCarros
{
    public class Db
    {
        private static string connString
        {
            get
            {
                return ConfigurationManager.ConnectionStrings["ConnString"].ConnectionString;
            }
        }

        public static DataSet ExecutarSQL(string ComandoSQL)
        {
            SqlConnection conn = new SqlConnection(connString);
            DataSet ds = new DataSet();

            try
            {
                using (SqlCommand cmd = new SqlCommand(ComandoSQL, conn))
                {
                    cmd.CommandType = CommandType.Text;
                    conn.Open();

                    SqlDataAdapter adap = new SqlDataAdapter(cmd);
                    adap.Fill(ds);

                    conn.Close();
                }
                return ds;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (ds != null)
                {
                    ds.Dispose();
                    ds = null;
                }

                if (conn != null)
                {
                    if (conn.State != ConnectionState.Closed)
                        conn.Close();

                    conn.Dispose();
                    conn = null;
                }
            }
        }
        public static void ExecutarComando(string ComandoSQL)
        {
            SqlConnection conn = new SqlConnection(connString);

            try
            {
                using (SqlCommand cmd = new SqlCommand(ComandoSQL, conn))
                {
                    cmd.CommandType = CommandType.Text;
                    conn.Open();

                    cmd.ExecuteNonQuery();

                    conn.Close();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (conn != null)
                {
                    if (conn.State != ConnectionState.Closed)
                        conn.Close();

                    conn.Dispose();
                    conn = null;
                }
            }
        }
    }
}